package com.songcha.sakura.mvvm.home.home

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.songcha.library_base.interfaces.IApiResult
import com.songcha.library_base.interfaces.IResult
import com.songcha.library_base.mvvm.base.BaseRepository
import com.songcha.library_base.mvvm.base.BaseViewModel
import com.songcha.library_common.helper.SharedPreferenceHelper
import com.songcha.sakura.bean.ComicListBean
import com.songcha.sakura.bean.HomeBannerListBean
import com.songcha.sakura.config.BaseConfig
import com.songcha.sakura.event.HomeComicGetCompletedEvent
import com.songcha.library_database_sakura.greendao.SearchHistoryBean
import com.songcha.library_database_sakura.greendao.SearchHistoryBeanDao


class HomeViewModel(app: Application): BaseViewModel<HomeRepository>(app) {

    val cateTitles=arrayListOf("周表","热血","格斗","恋爱","校园","搞笑","萝莉","神魔","机战","科幻","青春","魔法","美少女","后宫")
    private val _bannerList =  MutableLiveData<HomeBannerListBean>()
    val bannerList: MutableLiveData<HomeBannerListBean> = _bannerList
    val searchHistoryList=MutableLiveData<MutableList<SearchHistoryBean>>()
    private var update_cache_list=mutableMapOf<String,ComicListBean>()

    fun getSearchHistory(){
        handleDataObserver(repository!!.getSearchHistory(),object :IResult<MutableList<SearchHistoryBean>>{
            override fun onSuccess(data: MutableList<SearchHistoryBean>) {
                searchHistoryList.value=data
            }
        },true)

    }


    fun getBannerList(){
         val cacheTime= SharedPreferenceHelper.getInt("home_cache","banner_cache_time")
         if((System.currentTimeMillis()/1000)-cacheTime>=BaseConfig.HOME_CACHE_UPDATE_TIME){
             handleApiDataObserver((repository!!).getBannerList(),object :IApiResult<HomeBannerListBean>{                 override fun onSuccess(bean: HomeBannerListBean) {
                     _bannerList.value=bean
                     (repository!!).saveBannerListDB(bean)
                 }
             })
         }else{
             handleApiDataObserver((repository!!).updateBannerList(),object :
                 IApiResult<HomeBannerListBean> {
                 override fun onSuccess(bean: HomeBannerListBean) {
                     _bannerList.value=bean
                 }
             },true)
         }


     }


    fun updateHomeCache(event: HomeComicGetCompletedEvent){
        //更新首页缓存
        if(event.comicListBean.data.size==0) return
        if(update_cache_list.keys.size>=2) return

        val cacheTime= SharedPreferenceHelper.getInt("home_cache","cache_time")
        if((System.currentTimeMillis()/1000)-cacheTime<BaseConfig.HOME_CACHE_UPDATE_TIME){
            return
        }

        if(event.index<=3) {
            update_cache_list["${event.index}"] = event.comicListBean
        }

        if(update_cache_list.keys.size==2){
            ( repository as HomeRepository).updateHomeCache(update_cache_list["1"] as ComicListBean,update_cache_list["2"] as ComicListBean)
        }
    }


}


